package com.ren.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ReclFeignConfig
 * @Description: TODO
 * @Author: RZY
 * @DATE: 2022/7/18 17:49
 * @Version: v1.0
 */

@Configuration
public class ReclFeignConfig {

    @Bean
    RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            System.out.println("Feign请求拦截过滤---------------->");
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            //获取request对象
            HttpServletRequest request = attributes.getRequest();
            //获取Cookie
            String cookie = request.getHeader("Cookie");
            //放入feign的requestTemplate
            requestTemplate.header("Cookie", cookie);
        };
    }
}
